package com.gwq.computed;

import java.util.*;

/*
* 
**/

public class Compute {
    public static void main(String[] args) {

        /*short m=0,n=0,ov=0;
        char o='+';
        Random random = new Random();
        for (int i=0;i<50;i++){
            ov = (short) random.nextInt(2);
            m = (short)random.nextInt(101);
            n = (short)random.nextInt(101);
            if(ov==1){
                o = '+';
            }else {
                o = '-';
            }
            System.out.print(""+(i+1)+":\t"+m+o+n+"=");
            if(ov==1){
                System.out.println(m+n);
            }else{
                System.out.println(m-n);
            }
        }*/

        Map<Integer,String> map = new HashMap<Integer, String>();
        List<Integer> list = new ArrayList<>();

        List<String> setList = new ArrayList<>();

        printHeader();
        generateEquations(setList,list);
        printExercise(setList);
        printCalculations(list);
    }

    public static void printHeader(){
        System.out.println("--------------------------------------------------------");
        System.out.println("-  程序输出50道100以内的加减法算式的习题  -");
        System.out.println("-  每次运行程序可得到一套50道题的习题答案  -");
        System.out.println("--------------------------------------------------------");
    }



    public static  void generateEquations(List<String> setList,List<Integer> list){

        char o='+';

        for(int i=0;i<50;i++){
            Random random = new Random();
            Integer num1 = random.nextInt(101);
            Integer num2 = random.nextInt(101);
            Integer ov =  random.nextInt(2);
            Integer count = 0;

            if(ov==1){
                o = '+';
                count = num1+num2;
            }else {
                o = '-';
                count = num1-num2;
            }
            String compute = "";

            compute = num1+""+o+""+num2+"=";

            int y = 0;

            if(setList!=null){
                for(int j=0;j<setList.size();j++){

                    String compute1 =setList.get(j);

                    String f = "";
                    String f1 = "";
                    String f2 = "";

                    if(setList.get(j).contains("-")){
                        f = compute1.substring(compute1.indexOf("-"),compute1.indexOf("-")+1);
                        f1 = compute1.substring(0,compute1.indexOf("-"));
                        f2 = compute1.substring(compute1.indexOf("-")+1,compute1.indexOf("="));
                    }else{
                        f = compute1.substring(compute1.indexOf("+"),compute1.indexOf("+")+1);
                        f1 = compute1.substring(0,compute1.indexOf("+"));
                        f2 = compute1.substring(compute1.indexOf("+")+1,compute1.indexOf("="));
                    }


                    if(f1.equals(num2)&&f.equals(o)&&f2.equals(num1)){
                        i--;
                        y=1;
                    }
                }
            }

            if(y==0&&!setList.contains(compute)){
                if(count>100||count<0){
                    i--;
                }else{
                    list.add(count);
                    setList.add(compute);
                }
            }else{
                i--;
            }


        }
    }



    public static void printExercise(List<String> setList){
        int count = 0;
        for(int i=0;i<50;i++){
            System.out.print(i+1+":   "+setList.get(i)+ "          " + "\t");
            count++;
            if (count % 5 == 0) {
                System.out.println();
            }
        }
    }

    public static void printCalculations(List<Integer> list){
        System.out.println("--------------------------------------------------------");
        System.out.println("-  下面是习题的参考答案  -");
        System.out.println("--------------------------------------------------------");
        int count = 0;
        for(int i=0;i<list.size();i++){
            System.out.print(i+1+":   "+list.get(i)+ "          " + "\t");
            count++;
            if (count % 5 == 0) {
                System.out.println();
            }
        }
    }
}
